package com.aphrodite.io.sip.transaction;

import com.aphrodite.io.sip.message.Message;
import com.aphrodite.io.sip.provider.MethodIdentifier;
import com.aphrodite.io.sip.provider.SipProvider;
import com.aphrodite.io.sip.provider.SipProviderListener;

//import org.zoolu.tools.LogLevel;

/**
 * TransactionServerAccepter listens for new incoming transaction requests.
 */
public class TransactionServerAccepter implements SipProviderListener {
	/** TransactionServerAccepterListener that captures new transactions. */
	TransactionServerAccepterListener listener;

	/** Method name. */
	String method;

	/** SipProvider that receives incoming transaction requests.. */
	SipProvider sip_provider;

	/**
	 * Creates a new TransactionServerAccepter of type <i>method</i>, and starts
	 * listening for incoming trasaction requests.
	 */
	public TransactionServerAccepter(SipProvider sip_provider, String method, TransactionServerAccepterListener listener) {
		this.listener = listener;
		this.method = method;
		sip_provider.addSipProviderListener(new MethodIdentifier(method), this);
		// printLog("method: "+method,LogLevel.HIGH);
		// printLog("created",LogLevel.HIGH);
	}

	/** Stops listening for incoming trasaction requests. */
	public void halt() {
		sip_provider.removeSipProviderListener(new MethodIdentifier(method));
	}

	/** Method derived from interface SipListener. */
	public void onReceivedMessage(SipProvider provider, Message msg) {
		if (listener != null)
			listener.onNewTransactionServer(this, new TransactionServer(sip_provider, msg, listener));
	}

	// **************************** Logs ****************************/

	/** Adds a new string to the default Log */
	// protected void printLog(String str, int level)
	// { if (log!=null)
	// log.println("TransactionServerAccepter("+method+"): "+str
	// ,level+SipStack.LOG_LEVEL_TRANSACTION);
	// }
}
